| Conditions | 7 |
| Total Lines | 51 |
| Code Lines | 41 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { QueryHandler } from '@nestjs/cqrs'; |
||
| 27 | |||
| 28 | public async execute( |
||
| 29 | query: GetMealTicketsPerMonthQuery |
||
| 30 | ): Promise<MealTicketsPerMonthView[]> { |
||
| 31 | const cooperative = await this.cooperativeRepository.find(); |
||
| 32 | if (!cooperative) { |
||
| 33 | throw new CooperativeNotFoundException(); |
||
| 34 | } |
||
| 35 | |||
| 36 | const { date } = query; |
||
| 37 | const [users, mealTicketRemovals] = await Promise.all([ |
||
| 38 | this.userRepository.findUsers(false, true), |
||
| 39 | this.mealTicketRemovalRepository.findByMonth(date) |
||
| 40 | ]); |
||
| 41 | |||
| 42 | const events: FindAllEventsByMonth[] = await this.eventRepository.findAllEventsByMonth( |
||
| 43 | date, |
||
| 44 | [EventType.OTHER] |
||
| 45 | ); |
||
| 46 | |||
| 47 | const mealTicketsByUser = []; |
||
| 48 | const mealTicketsRemovalsByUser = []; |
||
| 49 | const mealTicketsPerMonthView: MealTicketsPerMonthView[] = []; |
||
| 50 | |||
| 51 | for (const { duration, user } of events) { |
||
| 52 | if (duration > cooperative.getDayDuration() / 2) { |
||
| 53 | mealTicketsByUser[user] = mealTicketsByUser[user] + 1 || 1; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | for (const { id, count } of mealTicketRemovals) { |
||
| 58 | mealTicketsRemovalsByUser[id] = count; |
||
| 59 | } |
||
| 60 | |||
| 61 | for (const user of users) { |
||
| 62 | const mealTicketRemoval = mealTicketsRemovalsByUser[user.getId()] || 0; |
||
| 63 | const mealTicket = |
||
| 64 | mealTicketsByUser[user.getId()] - mealTicketRemoval || 0; |
||
| 65 | |||
| 66 | mealTicketsPerMonthView.push( |
||
| 67 | new MealTicketsPerMonthView( |
||
| 68 | user.getId(), |
||
| 69 | user.getFirstName(), |
||
| 70 | user.getLastName(), |
||
| 71 | mealTicket <= 0 ? 0 : mealTicket, |
||
| 72 | mealTicketRemoval |
||
| 73 | ) |
||
| 74 | ); |
||
| 75 | } |
||
| 76 | |||
| 77 | return mealTicketsPerMonthView; |
||
| 78 | } |
||
| 80 |